package com.example.summer_pro.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.summer_pro.dao.QuesDao;
import com.example.summer_pro.dao.UserDao;
import com.example.summer_pro.entity.NormalsJSON;
import com.example.summer_pro.entity.Ques;
import com.example.summer_pro.service.NormalService;
import com.example.summer_pro.service.QuesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Closeable;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@Service
@Transactional
public class QuesServiceImpl implements QuesService {
	@Autowired
	private QuesDao quesDao;

	@Autowired
	private NormalService normalService;

	@Override
	public Ques getQuesByQid(Integer qid) {
		closeTimeoutQues();
		return quesDao.selectById(qid);
	}

	@Override
	public List<Ques> getQuesByUid(Integer uid) {
		closeTimeoutQues();
		return quesDao.selectList(new QueryWrapper<Ques>()
			.eq("`u_id`", uid)
			.eq("deleted", false)
		);
	}

	@Override
	public List<Ques> getQuesByKeyWord(Integer uid, String keyWord) {
		closeTimeoutQues();
		return quesDao.selectList(new QueryWrapper<Ques>()
			.like("`title`", keyWord)
			.eq("`u_id`", uid)
			.eq("deleted", false)
		);
	}
	@Override
	public List<Ques> getDeletedbyKeyWord(Integer uid, String keyWord) {
		closeTimeoutQues();
		return quesDao.selectList(new QueryWrapper<Ques>()
			.like("`title`", keyWord)
			.eq("`u_id`", uid)
			.eq("deleted", true)
		);
	}

	@Override
	public List<Ques> getDeletedbyUid(Integer uid) {
		closeTimeoutQues();
		return quesDao.selectList(new QueryWrapper<Ques>()
			.eq("`u_id`", uid)
			.eq("deleted", true)
		);
	}

	@Override
	public int delQues(Integer uid, Integer qid) {
		Ques ques = quesDao.selectById(qid);
		if (ques == null) { // 待确认：不存在的返回值
			return 1;
		}
		if (!uid.equals(ques.getU_id())) {
			return 2;
		}
		if (ques.getDeleted()){
			quesDao.deleteById(ques.getQ_id());
		} else {
			ques.setDelete_time(new Date());
			ques.setDeleted(true);
			// todo : 删除整合进state
			ques.setState(1);
			quesDao.updateById(ques);
		}
		return 0;
	}

	@Override
	public int createQues(Integer uid, Ques ques) {
		ques.setU_id(uid);
		ques.setCreate_time(new Date());
		ques.setRelease_time(null);
		ques.setLatest_release(null);
		ques.setRecover_num(0);
		ques.setTotal(0);
		ques.setState(1);
		ques.setDeleted(false);
		quesDao.insert(ques);
		return ques.getQ_id();
	}

	@Override
	public int unDeleteQues(Integer uid, Integer qid) {
		Ques ques = quesDao.selectById(qid);
		if (ques == null) {
			return 1;
		}
		if (!uid.equals(ques.getU_id())) {
			return 2;
		}
 		ques.setDelete_time(null);
		ques.setDeleted(false);
		quesDao.updateById(ques);
		return 0;
	}

	@Override
	public int setState(Integer uid, Integer qid, Integer state) {
		Ques ques = quesDao.selectById(qid);
		if (ques == null) {
			return 1;
		}
		if (!uid.equals(ques.getU_id())) {
			return 2;
		}
		if (state.equals(2)) {
			Date date = new Date();
			ques.setRelease_time(date);
			ques.setLatest_release(date);
		} else {
			ques.setValid_time(null);
		}
		ques.setState(state);
		quesDao.updateById(ques);
		return 0;
	}

	static Date last = new Date();
	public void closeTimeoutQues(){
		Date date = new Date();
		System.out.println("closeTimeoutQues runs gap " + (date.getTime() - last.getTime()));
		if (date.getTime() - last.getTime() > 30*1000) {
			synchronized (this) {
				System.out.println("closeTimeoutQues runs at" + date);
				quesDao.update(null, new UpdateWrapper<Ques>()
					.set("state", 1)
					.set("valid_time", null)
					.lt("valid_time", date)
				);
				last = date;
			}
		}
	}

	@Override
	public int setValidTime(Integer uid, Integer qid, Date date) {
		Ques ques = quesDao.selectById(qid);
		if (ques == null) {
			return 1;
		}
		if (!uid.equals(ques.getU_id())) {
			return 2;
		}
		ques.setValid_time(date);
		quesDao.updateById(ques);
		return 0;
	}

	Pattern copyPattern = Pattern.compile(".*的副本$");
	@Override
	public int copyQues(Integer uid, Integer qid) {
		Ques ques = quesDao.selectById(qid);
		if (ques == null) {
			throw new IllegalArgumentException("qid不存在");
		}
		if (!uid.equals(ques.getU_id())) {
			throw new IllegalArgumentException("qid不属于该用户");
		}
		ques.setQ_id(0);
		if (!copyPattern.matcher(ques.getTitle()).find())
			ques.setTitle(ques.getTitle() + "的副本");

		int new_qid = createQues(uid, ques);
		NormalsJSON normalsJSON=normalService.selectJson(qid);
		if (normalsJSON != null) {
			String jsonNow = normalsJSON.getJson();
			normalService.addJson(new_qid, jsonNow);
		}
		return new_qid;
	}
}
